﻿using PIMS.Models.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PIMS
{
    internal class DgvTools
    {
        /// <summary>
        /// dataGridView美化
        /// </summary>
        /// <param name="dataGridView"></param>
        public static void dataGridView(DataGridView dataGridView)
        {
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
            dataGridView.AllowUserToAddRows = false;
            dataGridView.AllowUserToDeleteRows = false;
            dataGridViewCellStyle1.BackColor = System.Drawing.Color.LightCyan;
            dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            dataGridView.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
            //所有单元格居中
            dataGridView.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dataGridView.BackgroundColor = System.Drawing.Color.White;
            dataGridView.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            dataGridView.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240
            dataGridViewCellStyle2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(211)))), ((int)(((byte)(223)))), ((int)(((byte)(240)))));
            dataGridViewCellStyle2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Navy;
            dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            dataGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle2;

            dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            dataGridView.EnableHeadersVisualStyles = false;
            dataGridView.GridColor = System.Drawing.SystemColors.GradientInactiveCaption;
            dataGridView.ReadOnly = true;
            dataGridView.RowHeadersVisible = false;
            dataGridView.RowTemplate.Height = 23;
            dataGridView.RowTemplate.ReadOnly = true;
            //dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            dataGridView.AllowUserToOrderColumns = false;
            dataGridView.AllowUserToResizeRows = false;
            dataGridView.AllowDrop = false;
        }
        /// <summary>
        /// 自动对齐
        /// </summary>
        /// <param name="dataGridView"></param>
        public static void dataGridViewAutoSize(DataGridView dgv)
        {
            //自动列宽
            dgv.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            //根据内容自动列宽
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
        }
        /// <summary>
        /// 禁用排序
        /// </summary>
        /// <param name="dgv"></param>
        public static void dataGridViewDisableSort(DataGridView dgv)
        {
            //DataGridView禁止排序
            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                dgv.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }

        /// <summary>
        /// 将DataGridView里面的数据提取到DataTable中
        /// </summary>
        /// <param name="dataGridView"></param>
        /// <returns></returns>
        public static DataTable DataGridViewToDataTable(DataGridView dgv)
        {
            var dataTable = new DataTable();

            for (var i = 0; i < dgv.RowCount; i++)
            {
                var dr = dataTable.NewRow();
                for (var j = 0; j < dgv.ColumnCount; j++)
                {
                    if (i == 0)
                    {
                        var dc = new DataColumn(dgv.Columns[j].Name);
                        dataTable.Columns.Add(dc);
                    }

                    dr[j] = dgv[j, i].Value;
                }
                dataTable.Rows.Add(dr);
            }
            return dataTable;
        }

        /// <summary>
        /// 根据XML配置文件自动生成列
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="configX"></param>
        public static void AutoGenerateColumns(DataGridView dgv, FeatureInfoX configX, List<string> exclude = null)
        {
            if (dgv.Columns.Count == 0)
            {
                dgv.AutoGenerateColumns = true;

                foreach (XItems item in configX.item)
                {
                    if (item.enable)
                    {
                        if (exclude == null || !exclude.Contains(item.name))
                        {
                            DataGridViewTextBoxColumn textBox = new DataGridViewTextBoxColumn();
                            textBox.HeaderText = item.dispName;
                            textBox.Name = item.name;
                            textBox.DataPropertyName = item.name;
                            textBox.ReadOnly = true;
                            dgv.Columns.Add(textBox);
                        }
                    }
                }
                dgv.AutoGenerateColumns = false; //锁定列名
            }
        }
    }
}
